package cn.anquing.demo.elasticsearch.demo.es;

import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.GetQuery;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;


/**
 * Query DSL 参考：https://www.pianshen.com/article/1084140324/
 * 4查询
 * GET index/type/_search
 * {
 * “query”:{
 * “bool”:{
 * “must”:{
 * “term”:{
 * “field”:“value”
 * }
 * }
 * }
 * }
 * }
 * query同级关键字有size，sort，aggs；bool同级的有rang，must同级should，term同级wild等等，格式大同小异
 */

/**
 * @author wangqq65
 * @since 2021/4/20 18:05
 */
@Service
public class EsBlogService {


    @Resource
    private EsBlogRepository esBlogRepository;

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate ;


    /**
     * getById.
     *
     * <p>使用ElasticsearchRepository实现.
     *
     * @author wangqq65
     * @since 2021/4/21 17:15
     * @param id
     * @return 
     */
    public EsBlog getById(Long id){

        Optional<EsBlog> optionalEsBlog = esBlogRepository.findById(id);

        if (optionalEsBlog.isPresent()){
            return optionalEsBlog.get();
        }
        return new EsBlog();
    }

    /**
     * getById2.
     *
     * <p>使用elasticsearchRestTemplate实现.
     *
     * @author wangqq65
     * @since 2021/4/21 17:16
     * @param id
     * @return
     */
    public EsBlog getById2(Long id){
        return elasticsearchRestTemplate.get(id + "", EsBlog.class);
    }


    public Page<EsBlog> searchByKeyword(String keyword) {


        //查询表达式(Query DSL)、
//        post /blog/_search
//        {"query":
//            {
//                "bool": {
//                "should": [
//
//                {
//                    "match_phrase": {
//                    "title": "ABC"
//                }
//                },
//                {
//                    "match_phrase": {
//                    "content": "生产"
//                }
//                }
//    ]
//            }
//            }
//        }

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("title",keyword));
        boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("content",keyword));
        String s = boolQueryBuilder.toString();
        System.out.printf("boolQueryBuilder.toString="+s);
        Page<EsBlog> search = (Page<EsBlog>) esBlogRepository.search(boolQueryBuilder);
        return search;
    }



    public SearchHits<EsBlog> searchByKeyword2(String keyword) {

        Criteria criteria = new Criteria("title").is(keyword).or("content").is(keyword);
        Query query = new CriteriaQuery(criteria);
        SearchHits<EsBlog> searchHits = elasticsearchRestTemplate.search(query, EsBlog.class);
        return searchHits;

    }
}

